package com.tbit.main.pojo.maintain_performance.group;

import com.tbit.main.constant.enums.ConditionLogic;
import com.tbit.main.constant.enums.PerformanceBizType;
import com.tbit.main.constant.enums.PerformanceRuleType;
import com.tbit.main.constant.enums.RuleLogic;
import com.tbit.main.pojo.PerformanceRule;
import com.tbit.main.pojo.PerformanceRuleCondition;
import com.tbit.main.pojo.PerformanceRuleGroup;
import com.tbit.main.pojo.maintain_performance.BizRuleGroup;
import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;

/**
 * @author <a href="mailto:zheng.zebiao@tbit.com.cn">zb</a>
 * @since 2025/2/21
 */
@Data
public class MoveBasicPerformanceGroup implements BizRuleGroup {
    /**
     * 是否启用
     */
    private Boolean enabled;
    /**
     * 站外挪车有效绩效
     */
    private BigDecimal outOfParkValidPerformance;
    /**
     * 超区挪车有效绩效
     */
    private BigDecimal outOfGeoValidPerformance;

    @Override
    public PerformanceRuleGroup toRaw(PerformanceBizType bizType, Integer accountId) {
        PerformanceRuleGroup group = new PerformanceRuleGroup();
        group.setAccountId(accountId);
        group.setBizType(bizType);
        group.setRuleType(PerformanceRuleType.BASIC);
        group.setEnabled(enabled);
        group.setRules(new ArrayList<>(2));

        PerformanceRule outOfParkRule = new PerformanceRule();
        outOfParkRule.setPerformance(outOfParkValidPerformance);
        outOfParkRule.setConditionLogic(ConditionLogic.AND);
        outOfParkRule.setConditions(Collections.singletonList(
                new PerformanceRuleCondition() {{
                    setKey("moveType");
                    setLogic(RuleLogic.EQUAL);
                    setValue("outOfPark");
                }}
        ));
        group.getRules().add(outOfParkRule);

        PerformanceRule outOfGeoRule = new PerformanceRule();
        outOfGeoRule.setPerformance(outOfGeoValidPerformance);
        outOfGeoRule.setConditionLogic(ConditionLogic.AND);
        outOfGeoRule.setConditions(Collections.singletonList(
                new PerformanceRuleCondition() {{
                    setKey("moveType");
                    setLogic(RuleLogic.EQUAL);
                    setValue("outOfGeo");
                }}
        ));
        group.getRules().add(outOfGeoRule);

        return group;
    }

    @Override
    public void initFromRaw(PerformanceRuleGroup group) {
        setEnabled(group.getEnabled());
        for (PerformanceRule rule : group.getRules()) {
            for (PerformanceRuleCondition condition : rule.getConditions()) {
                if (!"moveType".equals(condition.getKey()))
                    continue;

                if ("outOfPark".equals(condition.getValue())) {
                    setOutOfParkValidPerformance(rule.getPerformance());
                } else if ("outOfGeo".equals(condition.getValue())) {
                    setOutOfGeoValidPerformance(rule.getPerformance());
                }
            }
        }
    }
}
